package com.agan.workflow.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.agan.common.core.utils.StringUtils;
import com.agan.common.core.web.domain.ResponseResult;
import com.agan.workflow.domain.BpmType;
import com.agan.workflow.service.IBpmTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.agan.common.log.annotation.Log;
import com.agan.common.log.enums.BusinessType;
import com.agan.common.security.annotation.RequiresPermissions;
import com.agan.workflow.domain.BpmDefinition;
import com.agan.workflow.service.IBpmDefinitionService;
import com.agan.common.core.web.controller.BaseController;
import com.agan.common.core.utils.poi.ExcelUtil;
import com.agan.common.core.web.page.TableDataInfo;

/**
 * 流程定义Controller
 * 
 * @author agan
 * @date 2024-01-11
 */
@RestController
@RequestMapping("/definition")
@RequiredArgsConstructor
public class BpmDefinitionController extends BaseController
{
    private final IBpmDefinitionService bpmDefinitionService;
    private final IBpmTypeService bpmTypeService;

    /**
     * 查询流程定义列表
     */
    @RequiresPermissions("workflow:definition:list")
    @GetMapping("/list")
    public TableDataInfo list(BpmDefinition bpmDefinition)
    {
        return getDataTable(bpmDefinitionService.selectBpmDefinitionList(bpmDefinition));
    }

    /**
     * 导出流程定义列表
     */
    @RequiresPermissions("workflow:definition:export")
    @Log(title = "流程定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BpmDefinition bpmDefinition)
    {
        List<BpmDefinition> list = bpmDefinitionService.selectBpmDefinitionList(bpmDefinition).getRecords();
        ExcelUtil<BpmDefinition> util = new ExcelUtil<BpmDefinition>(BpmDefinition.class);
        util.exportExcel(response, list, "流程定义数据");
    }

    /**
     * 获取流程定义详细信息
     */
    @RequiresPermissions("workflow:definition:query")
    @GetMapping(value = "/{defId}")
    public ResponseResult getInfo(@PathVariable("defId") Long defId)
    {
        BpmDefinition bpmDefinition = bpmDefinitionService.selectBpmDefinitionByDefId(defId);
        bpmDefinition.setDefXml(StringUtils.base64decode(bpmDefinition.getDefXml()));
        //logger.info("bpmDefinition defXml = {} ", bpmDefinition.getDefXml());
        return success(bpmDefinition);
    }

    /**
     * 新增流程定义
     */
    @RequiresPermissions("workflow:definition:add")
    @Log(title = "流程定义", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseResult add(@RequestBody BpmDefinition bpmDefinition)
    {
        bpmDefinition.setDefXml(StringUtils.base64decode(bpmDefinition.getDefXml()));
        //logger.info("bpmDefinition defXml = {} ", bpmDefinition.getDefXml());
        return toResponse(bpmDefinitionService.insertBpmDefinition(bpmDefinition));
    }

    /**
     * 修改流程定义
     */
    @RequiresPermissions("workflow:definition:edit")
    @Log(title = "流程定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseResult edit(@RequestBody BpmDefinition bpmDefinition)
    {
        bpmDefinition.setDefXml(StringUtils.base64decode(bpmDefinition.getDefXml()));
        //logger.info("bpmDefinition defXml = {} ", bpmDefinition.getDefXml());
        return toResponse(bpmDefinitionService.updateBpmDefinition(bpmDefinition));
    }

    /**
     * 删除流程定义
     */
    @RequiresPermissions("workflow:definition:remove")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{defIds}")
    public ResponseResult remove(@PathVariable Long[] defIds)
    {
        return toResponse(bpmDefinitionService.deleteBpmDefinitionByDefIds(defIds));
    }


    /**
     * 流程定义树
     */
    @RequiresPermissions("workflow:definition:list")
    @GetMapping("/typeTree")
    public ResponseResult typeTree(BpmType bpmType)
    {
        return success(bpmTypeService.selectTypeTreeList(bpmType));
    }

}
